﻿@model Citi.HeadCount.WebUI.Models.RuleViewModel
<script type="text/javascript">
    $(function () {
        $("#AddRule").click(function () {
            var error = false;

            if ($("#Type :selected").text() == "-" || $("#Type :selected").text() == "") {
                alert("Field column is required!");
                return false;
            }

            if ($("#Type :selected").val() == "0" || $("#Type :selected").val() == "1") {
                if ($("#Level").val() == "" || $("#Level").val() == "0" || $("#Level").val() == "00") {
                    alert("Field level is required!");
                    return false;
                }
            }

            if ($("#Value").val() == "") {
                alert("Field level is required!");
                return false;
            }

            $("#tableRules").find('tr').each(function (index, item) {
                if (item.cells[1].innerHTML == $("#Type :selected").text()
                        && item.cells[2].innerHTML == $("#Level").val()) {
                    error = true;
                }
            });

            if (error == true) {
                alert("This rule already exists!");
                return false;
            };

            //$("#formRules").submit();

            var tr = '<tr><td style="display:none;">{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>';
            tr = tr.replace('{0}', '');
            tr = tr.replace('{1}', $('#Type :selected').text());
            tr = tr.replace('{2}', $('#Level').val());
            tr = tr.replace('{3}', $('#Value').val());
            tr = tr.replace('{4}', $('#Type :selected').val());

            $("#tableRules tr:last").after(tr);
        });

        $("#Type").change(function () {
            if ($("#Type :selected").text() != "@Citi.HeadCount.Domain.RuleType.ManagedSegment.GetDescription()"
                && $("#Type :selected").text() != "@Citi.HeadCount.Domain.RuleType.Function.GetDescription()") {
                $("#Level").val("");
                $("#Level").attr("disabled", "disabled");
            }
            else {
                $("#Level").val("");
                $("#Level").removeAttr("disabled");
            }
        });

        $("#Level").keydown(validateNumericField);

    });
</script>
@*@using (Ajax.BeginForm("Create", "Rule", new AjaxOptions { UpdateTargetId = "rules", OnComplete = "clearAllContentExceptHidden" }, new { id = "formRules" }))
{*@
    <fieldset>
        <legend>Create Rules</legend>
        <br />
        @Html.LabelFor(model => model.Type)
        @Html.DropDownListFor(model => model.Type, (List<SelectListItem>)ViewBag.RuleTypes)
        @Html.LabelFor(model => model.Level)
        @Html.TextBoxFor(model => model.Level, new { style = "width: 20px", maxlength = 2 })
        @Html.LabelFor(model => model.Value)
        @Html.TextBoxFor(model => model.Value, new { style = "width: 200px", maxlength = 45 })
        @Html.HiddenFor(model => Model.RuleResultId, new { @class = "hiddenRuleResultId" })
        <input type="button" value="Add" id="AddRule" style="margin-left:10px" />
    </fieldset>
@*}*@
